import Vue from 'vue'
import Router from 'vue-router'
import Tabbar from './components/tabbar'
import TabHome from './views/home/tab-home'
import TabCart from './views/cart/tab-cart'
import TabProducts from './views/product/tab-products.vue'
import TabSetting from './views/setting/tab-setting'
import { getLocalStorage } from './common/utils/local-storage'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        keepAlive: true
      },
      components: {
        default: TabHome,
        tabbar: Tabbar
      }
    },
    {
      path: '/cart',
      name: 'cart',
      components: {
        default: TabCart,
        tabbar: Tabbar
      }
    },
    {
      path: '/products',
      name: 'products',
      meta: {
        requireAuth: false
      },
      components: {
        default: TabProducts,
        tabbar: Tabbar
      }
    },
    {
      path: '/user',
      name: 'user',
      meta: {
        requireAuth: true
      },
      components: {
        default: TabSetting,
        tabbar: Tabbar
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('./views/account/login/login.vue')
    },
    {
      path: '/product/:id',
      name: 'product',
      component: () => import('./views/product/product.vue')
    },
    {
      path: '/account',
      name: 'account',
      component: () => import('./views/setting/account/account.vue')
    },
    {
      path: '/bind/:id',
      name: 'bind',
      meta: {
        // requireAuth: true
      },
      component: () => import('./views/setting/bind/bind.vue')
    },
    {
      path: '/scanner',
      name: 'scanner',
      meta: {
        // requireAuth: true
      },
      component: () => import('./views/setting/bind/scanner.vue')
    },
    // 查看报告
    {
      path: '/seereport',
      name: 'seereport',
      component: () => import('./views/setting/seereport/seereport.vue')
    },
    // 详细报告
    {
      path: '/detailreport',
      name: 'detailreport',
      component: () => import('./views/setting/seereport/detailreport.vue')
    },
    // 查看详情
    {
      path: '/seedetail',
      name: 'seedetail',
      component: () => import('./views/setting/seereport/seedetail.vue')
    },
    {
      path: '/seedetail-acne',
      name: 'seedetail-acne',
      component: () => import('./views/setting/seereport/seedetail-acne.vue')
    },
    {
      path: '/seedetail-moisture',
      name: 'seedetail-moisture',
      component: () => import('./views/setting/seereport/seedetail-moisture.vue')
    },
    {
      path: '/seedetail-sunscreen',
      name: 'seedetail-sunscreen',
      component: () => import('./views/setting/seereport/seedetail-sunscreen.vue')
    },
    {
      path: '/seedetail-whitening',
      name: 'seedetail-whitening',
      component: () => import('./views/setting/seereport/seedetail-whitening.vue')
    },
    {
      path: '/seedetail-wrinkle',
      name: 'seedetail-wrinkle',
      component: () => import('./views/setting/seereport/seedetail-wrinkle.vue')
    },
    {
      path: '/help',
      name: 'help',
      component: () => import('./views/setting/help/help.vue')
    },
    {
      path: '/report',
      name: 'report',
      component: () => import('./views/setting/report/report.vue')
    },
    {
      path: '/contactus',
      name: 'contactus',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/contactus/index.vue')
    },
    {
      path: '/orders',
      name: 'orders',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/setting/orders/orders.vue')
    },
    {
      path: '/payment/:id',
      name: 'payment',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/order/payment.vue')
    },
    {
      path: '/payment-confirm/:id',
      name: 'payment-confirm',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/order/payment-confirm.vue')
    },
    {
      path: '/payment-result',
      name: 'payment-result',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/order/payment-result.vue')
    },
    {
      path: '/addresses',
      name: 'addresses',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/order/address/address_list.vue')
    },
    {
      path: '/addresses/edit/:id',
      name: 'address-edit',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/order/address/address_edt.vue')
    },
    {
      path: '/addresses/add',
      name: 'address-add',
      meta: {
        requireAuth: true
      },
      component: () => import('./views/order/address/address_add.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requireAuth)) {
    const { token, username } = getLocalStorage(
      'token',
      'username'
    )
    console.log(token)
    console.log(username)
    if (!token && !username) {
      next({ name: 'login', query: { redirect: to.name } })
      return
    }
  }
  next()
})

export default router
